<template>
  <div class="shared-questions-page">
    <!-- 页面头部 -->
    <div class="page-header">
      <div class="header-content">
        <button @click="goBack" class="back-btn">← 返回管理中心</button>
        <h1 class="page-title">我共享给别人的试题</h1>
        <p class="page-subtitle">管理您分享给他人的试题</p>
      </div>
    </div>

    <!-- 主要内容区域 -->
    <main class="main-content">
      <div class="container">
        <!-- 搜索区域 -->
        <div class="search-section">
          <div class="search-row">
            <div class="form-group">
              <label>程序类型</label>
              <select v-model="searchForm.type" class="form-select">
                <option value="">全部</option>
                <option value="C++">C++</option>
                <option value="Python">Python</option>
                <option value="Java">Java</option>
                <option value="GOC">GOC</option>
              </select>
            </div>
            
            <div class="form-group">
              <label>题号</label>
              <input 
                v-model="searchForm.questionId" 
                type="text" 
                placeholder="请输入题号"
                class="form-input"
              />
            </div>
            
            <div class="form-group">
              <label>标题</label>
              <input 
                v-model="searchForm.title" 
                type="text" 
                placeholder="请输入关键词"
                class="form-input"
              />
            </div>
            
            <button @click="searchQuestions" class="search-btn">搜索</button>
          </div>
        </div>

        <!-- 试题列表标题 -->
        <div class="list-header">
          <h2 class="list-title">我共享的试题列表</h2>
        </div>

        <!-- 试题表格 -->
        <div class="table-container">
          <table class="questions-table">
            <thead>
              <tr>
                <th>题号</th>
                <th>试题类型</th>
                <th>标题</th>
                <th>共享时间</th>
                <th>共享人</th>
                <th>操作</th>
              </tr>
            </thead>
            <tbody>
              <tr v-if="questions.length === 0">
                <td colspan="6" class="empty-row">
                  暂无数据
                </td>
              </tr>
              <tr v-for="question in questions" :key="question.id" class="table-row">
                <td>{{ question.questionId }}</td>
                <td>{{ question.type }}</td>
                <td class="title-cell">{{ question.title }}</td>
                <td>{{ formatDate(question.shareTime) }}</td>
                <td>{{ question.sharedBy }}</td>
                <td class="action-cell">
                  <button @click="viewQuestion(question)" class="action-btn view-btn">查看</button>
                  <button @click="editQuestion(question)" class="action-btn edit-btn">编辑</button>
                  <button @click="deleteQuestion(question)" class="action-btn delete-btn">删除</button>
                </td>
              </tr>
            </tbody>
          </table>
        </div>

        <!-- 分页 -->
        <div class="pagination-container">
          <div class="pagination">
            <span class="page-info">(共 {{ totalCount }} 条)</span>
            <button 
              @click="goToPage(currentPage - 1)"
              :disabled="currentPage <= 1"
              class="page-btn"
            >
              上一页
            </button>
            <span class="current-page">{{ currentPage }}</span>
            <button 
              @click="goToPage(currentPage + 1)"
              :disabled="currentPage >= totalPages"
              class="page-btn"
            >
              下一页
            </button>
          </div>
        </div>
      </div>
    </main>
  </div>
</template>

<script>
import { ref, reactive, onMounted } from 'vue'
import logger from '../utils/logger.js'

export default {
  name: 'SharedQuestions',
  setup() {
    // 响应式数据
    const questions = ref([])
    const currentPage = ref(1)
    const totalPages = ref(1)
    const totalCount = ref(0)
    const pageSize = 10

    // 搜索表单
    const searchForm = reactive({
      type: '',
      questionId: '',
      title: ''
    })

    // 模拟数据（空列表）
    const mockQuestions = []

    // 方法
    const loadQuestions = () => {
      // 这里应该调用API获取数据
      // 暂时使用模拟数据
      questions.value = mockQuestions
      totalCount.value = mockQuestions.length
      totalPages.value = Math.ceil(totalCount.value / pageSize)
    }

    const searchQuestions = () => {
      logger.log('搜索参数:', searchForm)
      // 执行搜索逻辑
      loadQuestions()
    }

    const formatDate = (dateStr) => {
      if (!dateStr) return ''
      const date = new Date(dateStr)
      return date.toLocaleDateString('zh-CN') + ' ' + date.toLocaleTimeString('zh-CN', { hour12: false })
    }

    const goToPage = (page) => {
      if (page >= 1 && page <= totalPages.value) {
        currentPage.value = page
        loadQuestions()
      }
    }

    const viewQuestion = (question) => {
      logger.log('查看试题:', question)
      // 跳转到试题详情页
    }

    const editQuestion = (question) => {
      logger.log('编辑试题:', question)
      // 跳转到试题编辑页
    }

    const deleteQuestion = (question) => {
      if (confirm(`确定要删除试题"${question.title}"吗？`)) {
        logger.log('删除试题:', question)
        // 执行删除逻辑
        loadQuestions()
      }
    }

    const goBack = () => {
      // 返回管理中心
      window.history.back()
    }

    // 生命周期
    onMounted(() => {
      loadQuestions()
    })

    return {
      questions,
      currentPage,
      totalPages,
      totalCount,
      searchForm,
      searchQuestions,
      formatDate,
      goToPage,
      viewQuestion,
      editQuestion,
      deleteQuestion,
      goBack
    }
  }
}
</script>

<style scoped>
.shared-questions-page {
  min-height: 100vh;
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
  padding: 20px;
}

/* 页面头部样式 */
.page-header {
  background: white;
  border-radius: 16px;
  box-shadow: 0 4px 20px rgba(0,0,0,0.1);
  padding: 30px;
  margin-bottom: 30px;
}

.header-content {
  max-width: 1200px;
  margin: 0 auto;
}

.back-btn {
  background: #3498db;
  color: white;
  border: none;
  padding: 8px 16px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 14px;
  margin-bottom: 20px;
  transition: background 0.2s;
}

.back-btn:hover {
  background: #2980b9;
}

.page-title {
  font-size: 32px;
  color: #2c3e50;
  margin: 0 0 10px 0;
  font-weight: 700;
}

.page-subtitle {
  font-size: 16px;
  color: #7f8c8d;
  margin: 0;
}

/* 主要内容区域 */
.main-content {
  max-width: 1200px;
  margin: 0 auto;
}

.container {
  background: white;
  border-radius: 16px;
  box-shadow: 0 4px 20px rgba(0,0,0,0.1);
  padding: 30px;
}

/* 搜索区域 */
.search-section {
  background: white;
  padding: 20px;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.1);
  margin-bottom: 20px;
}

.search-row {
  display: flex;
  gap: 20px;
  align-items: end;
}

.form-group {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.form-group label {
  font-size: 14px;
  color: #333;
  font-weight: 500;
}

.form-select,
.form-input {
  padding: 8px 12px;
  border: 1px solid #ddd;
  border-radius: 4px;
  font-size: 14px;
  min-width: 150px;
}

.search-btn {
  background: #4CAF50;
  color: white;
  border: none;
  padding: 8px 24px;
  border-radius: 4px;
  cursor: pointer;
  font-size: 14px;
  height: 36px;
}

.search-btn:hover {
  background: #45a049;
}

/* 列表标题 */
.list-header {
  margin: 20px 0;
}

.list-title {
  font-size: 18px;
  color: #333;
  margin: 0;
  padding-left: 8px;
  border-left: 4px solid #4CAF50;
}

/* 表格样式 */
.table-container {
  background: white;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 2px 8px rgba(0,0,0,0.1);
}

.questions-table {
  width: 100%;
  border-collapse: collapse;
}

.questions-table th {
  background: #4CAF50;
  color: white;
  padding: 15px 20px;
  text-align: left;
  font-weight: 500;
  font-size: 14px;
}

.questions-table td {
  padding: 15px 20px;
  border-bottom: 1px solid #eee;
  font-size: 14px;
  color: #333;
}

.table-row:hover {
  background: #f9f9f9;
}

.title-cell {
  max-width: 300px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.action-cell {
  white-space: nowrap;
}

.action-btn {
  padding: 4px 12px;
  margin: 0 2px;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 12px;
}

.view-btn {
  background: #2196F3;
  color: white;
}

.edit-btn {
  background: #FF9800;
  color: white;
}

.delete-btn {
  background: #f44336;
  color: white;
}

.action-btn:hover {
  opacity: 0.8;
}

.empty-row {
  text-align: center;
  color: #999;
  padding: 40px 20px;
}

/* 分页样式 */
.pagination-container {
  display: flex;
  justify-content: center;
  margin: 20px 0;
}

.pagination {
  display: flex;
  align-items: center;
  gap: 10px;
  background: white;
  padding: 10px 20px;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.1);
}

.page-info {
  color: #666;
  font-size: 14px;
}

.page-btn {
  padding: 6px 12px;
  border: 1px solid #ddd;
  background: white;
  cursor: pointer;
  border-radius: 4px;
  font-size: 14px;
}

.page-btn:hover:not(:disabled) {
  background: #f5f5f5;
}

.page-btn:disabled {
  color: #ccc;
  cursor: not-allowed;
}

.current-page {
  background: #4CAF50;
  color: white;
  padding: 6px 12px;
  border-radius: 4px;
  font-size: 14px;
  min-width: 32px;
  text-align: center;
}



/* 响应式设计 */
@media (max-width: 768px) {
  .shared-questions-page {
    padding: 10px;
  }
  
  .page-header {
    padding: 20px;
  }
  
  .page-title {
    font-size: 24px;
  }
  
  .container {
    padding: 20px;
  }
  
  .search-row {
    flex-direction: column;
    align-items: stretch;
  }
  
  .questions-table {
    font-size: 12px;
  }
  
  .questions-table th,
  .questions-table td {
    padding: 10px;
  }
}
</style> 